package com.xhu.collegestudentrentalsystem.module.user.controller;

import com.xhu.collegestudentrentalsystem.annocation.LoginRequired;
import com.xhu.collegestudentrentalsystem.module.user.entity.User;
import com.xhu.collegestudentrentalsystem.module.user.service.impl.UserServiceImpl;
import com.xhu.collegestudentrentalsystem.util.FileUtils;
import com.xhu.collegestudentrentalsystem.util.HostHolder;
import com.xhu.collegestudentrentalsystem.util.ResultData;
import com.xhu.collegestudentrentalsystem.util.constant.EnumProperty;
import com.xhu.collegestudentrentalsystem.util.constant.KeyMsg;
import com.xhu.collegestudentrentalsystem.util.constant.PointMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author zhu huilin
 * @date 2023/3/15 13:25
 */
@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserServiceImpl userService;

    @Autowired
    private HostHolder hostHolder;

    /**
     * 用户个人主页信息展示
     */
    @LoginRequired
    @RequestMapping("/info")
    public String getUserInfo(Model model){
        // 查询用户个人信息
        User user = userService.findUserById(hostHolder.getUser().getId());
        model.addAttribute(KeyMsg.user.USER, user);
        return "site/info";
    }

    /**
     * 用户修改个人信息
     */
    @RequestMapping("/update")
    public String updateUserInfo(User user, @RequestParam("file")MultipartFile file, Model model){
        // 头像上传位置，如果没有传头像，就不修改头像信息
        String realPath = this.getClass().getResource("/").getPath() + EnumProperty.fileUpload.USER_HEAD_URL;
        try {
            String filename = FileUtils.uploadOneFile(file, realPath);
            // 如果没有上传头像，则这里将头像设置为空，后续判断是否为空就可以决定是否修改头像字段
            user.setUserHead(filename);
        } catch (IOException e) {
            e.printStackTrace();
        }
        User updateUser = userService.updateUserInfo(user);
        model.addAttribute(KeyMsg.user.USER, updateUser);
        return "site/info";
    }

    /**
     * 修改自己的密码
     */
    @RequestMapping("/updatePassword")
    @ResponseBody
    public ResultData updatePassword(String oldPassword, String newPassword, HttpServletResponse response){
        if(hostHolder.getUser() == null){
            return new ResultData(ResultData.LOGIN_FAIL, PointMsg.login.UN_LOGIN);
        }
        if(!hostHolder.getUser().getPassword().equals(oldPassword)){
            return new ResultData(ResultData.OPERATION_FAIL, PointMsg.password.ERROR_OLD_PASSWORD);
        }
        Integer result = userService.updatePassword(hostHolder.getUser().getId(), newPassword);
        if(result > 0){
            return new ResultData(ResultData.OPERATION_SUCCESS, PointMsg.password.UPDATE_PASSWORD_SUCCESS);
        }
        return new ResultData(ResultData.OPERATION_FAIL, PointMsg.password.UPDATE_PASSWORD_FAIL);
    }

}
